package com.jn.algorithm.day004.question13;

import java.util.HashMap;
import java.util.Map;

import static javax.swing.UIManager.put;

/**
 * @author 江南大学1033190417
 * @date 2022/4/18 16:47
 */
public class Solution {

    Map<Character, Integer> symbolValues = new HashMap<Character, Integer>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};

    public int romanToInt(String s) {
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i < s.length() - 1) {
                Integer now = symbolValues.get(s.charAt(i));
                Integer after = symbolValues.get(s.charAt(i + 1));
                if (now < after) {
                    res -= now;
                } else {
                    res += now;
                }
            } else {
                res += symbolValues.get(s.charAt(i));
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "MCMXCIV";
        System.out.println(solution.romanToInt(s));
    }
}
